//
//  ViewController.m
//  图片在UITableView中滚动
//
//  Created by 周成龙 on 2018/2/8.
//  Copyright © 2018年 XZL. All rights reserved.
//

#import "ViewController.h"

static NSString *cellID = @"cell";

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tabelView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabelView.tableFooterView = [UIView new];
    
//    self.tabelView.estimatedRowHeight = 60;
//    self.tabelView.rowHeight = UITableViewAutomaticDimension;
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    [self scrollViewDidScroll:self.tabelView];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 300;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tabelView) {
        for (UITableViewCell *cell in self.tabelView.visibleCells) {
            UIView *bgView = [cell.contentView viewWithTag:1000];
            UIImageView *iv = [cell.contentView viewWithTag:1001];
            
            CGRect rect = [bgView convertRect:bgView.bounds toView:nil];
            CGFloat Y = [UIScreen mainScreen].bounds.size.height - rect.origin.y - 600;
            
            Y *= 0.2;
            if (Y > 0) {
                Y = 0;
            }
            if (Y < -100) {
                Y = -100;
            }
            
            CGRect ivFrame = iv.frame;
            ivFrame.origin.y = Y;
            iv.frame = ivFrame;
        }
    }
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    self.tabelView.separatorInset = UIEdgeInsetsZero;
    self.tabelView.layoutMargins = UIEdgeInsetsZero;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

@end
